package name.wilbur.course.db.bigone.control;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import name.wilbur.course.db.bigone.model.Article;
import name.wilbur.course.db.bigone.model.BaseDBModel;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.model.UserProfileView;
import name.wilbur.course.db.bigone.service.IArticleService;
import name.wilbur.course.db.bigone.service.IFriendService;
import name.wilbur.course.db.bigone.service.IPrepareService;
import name.wilbur.course.db.bigone.service.ISearchService;
import name.wilbur.course.db.bigone.service.IUserService;
import name.wilbur.course.db.bigone.service.impl.ArticleService;
import name.wilbur.course.db.bigone.service.impl.FriendService;
import name.wilbur.course.db.bigone.service.impl.PrepareService;
import name.wilbur.course.db.bigone.service.impl.SearchService;
import name.wilbur.course.db.bigone.service.impl.UserService;
import name.wilbur.course.db.bigone.ui.custom.ListModel;

public class MainMgr extends BaseMgr {
	private ISearchService searchServ;
	private IPrepareService prepareServ;
	private IUserService userServ;
	private IFriendService friendServ;
	private IArticleService articleServ;
	
	public MainMgr(){
		searchServ = new SearchService();
		prepareServ = new PrepareService();
		userServ = new UserService();
		friendServ = new FriendService();
		articleServ = new ArticleService();
	}
	
	public MainMgr(Map<String, Object> session) {
		this();
		this.session = session;
	}
	
	public boolean deleteArticle(Article article) {
		return articleServ.deleteArticle(article);
	}

	public ListModel<UserProfileView> searchFriend(String name, String cityName) {
		UserProfileView[] ufview = searchServ.searchWithNameCityView(name, cityName);
		if (ufview == null)
			return new ListModel<UserProfileView>();
		
		List<UserProfileView> userList = Arrays.asList(ufview);
		ListModel<UserProfileView> users = new ListModel<UserProfileView>(userList);
		return users;
	}
	
	public List<BaseDBModel> getArticles() {
		return articleServ.getAllArticles();
	}
	
	public String[] getAllCitys() {
		return prepareServ.getAllCityNames();
	}
	
	public boolean addFriend(UserProfileView friend) {
		if (friend != null)
			return friendServ.addNewFriend(getMe(), friend.getPk());
		else return false;
	}
	public boolean deleteFriend(int myID, int friendID) {
		return friendServ.removeFriend(myID, friendID);
	}
	
	public List<BaseDBModel> findAllFriends() {
		List<User> users = friendServ.findMyFriends(getMe().getPk());
		List<BaseDBModel> usersList = new ArrayList<BaseDBModel>(users.size());
		for (User u : users) {
			usersList.add(u);
		}
		return usersList;
	}
}
